﻿using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.IO;

namespace SVN.SourceCode.BackUp
{
    class FileBackupLogic
    {
        string sourceFolder, backupLocation = string.Empty;
        List<string> folderToSkip, extensionToSkip = null;

        public FileBackupLogic(string sourceFolder, string backupLocation,
            List<string> folderToSkip, List<string> extensionToSkip)
        {
            this.sourceFolder = sourceFolder;//.ToLower();
            this.backupLocation = backupLocation;
            this.folderToSkip = folderToSkip.ConvertAll<string>(x => x.ToLower().Trim());
            this.extensionToSkip = extensionToSkip.ConvertAll<string>(x => x.ToLower().Trim());
        }

        internal void ZipFile(System.ComponentModel.BackgroundWorker backgroundWorker1)
        {
            List<string> localfiles = new List<string>(Directory.GetFiles(
                sourceFolder, "*.*", SearchOption.AllDirectories));

            string tempFolderPath = Path.Combine(Path.GetTempPath(),
                                            Path.GetFileNameWithoutExtension(Path.GetTempFileName()));

            for (int i = 0; i < localfiles.Count; i++)
            {

                if (folderToSkip.Exists((x) => localfiles[i].ToLower().Contains("\\" + x + "\\")))
                {

                    backgroundWorker1.ReportProgress((i * 100) / localfiles.Count, localfiles[i] + " \nFolder skipped.");
                    Console.Out.WriteLine(localfiles[i]);
                    Console.Out.WriteLine("Folder skipped");
                    continue;
                }

                if (extensionToSkip.Exists((x) => localfiles[i].ToLower().EndsWith(x)))
                {
                    backgroundWorker1.ReportProgress((i * 100) / localfiles.Count, localfiles[i] + " \nFolder skipped.");
                    Console.Out.WriteLine(localfiles[i]);
                    Console.Out.WriteLine("File Extension skipped");
                    continue;
                }

                if ((i + "").EndsWith("00"))
                    backgroundWorker1.ReportProgress((i * 100) / localfiles.Count, "");
                
                File.Copy(localfiles[i], NewFilepath(localfiles[i], sourceFolder, tempFolderPath), true);

                if (backgroundWorker1.CancellationPending)
                    return;
                //Console.Out.WriteLine("Copied");
            }
            backgroundWorker1.ReportProgress(((localfiles.Count - 2) * 100) / localfiles.Count, "File getting compressed.. ..");

            ZipUtil zipFolder = new ZipUtil();

            if (backgroundWorker1.CancellationPending)
                return;

            string zipFile = zipFolder.CreateZipFile(tempFolderPath, sourceFolder);
            backgroundWorker1.ReportProgress((localfiles.Count * 100) / localfiles.Count, "File compressed & saved as\n" + zipFile);
            
        }

        private static string  NewFilepath(string localfiles, string sourceFolder, string tempFolderPath)
        {
            string newFilePath = localfiles.Replace(sourceFolder, tempFolderPath);
            if (Directory.Exists(Path.GetDirectoryName(newFilePath)) == false)
                Directory.CreateDirectory(Path.GetDirectoryName(newFilePath));
            return newFilePath;
        }

        
    }

}
